package com.pgytesting.secondhandcar.service.impl;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.pgytesting.secondhandcar.dao.OrderNameDao;
import com.pgytesting.secondhandcar.dao.RepaymentBillDao;
import com.pgytesting.secondhandcar.model.LoanRepaymentInformation;
import com.pgytesting.secondhandcar.model.OrderName;
import com.pgytesting.secondhandcar.model.RepaymentBill;
import com.pgytesting.secondhandcar.service.RepaymentBillService;
import com.pgytesting.secondhandcar.util.SessionUtil;
import com.pgytesting.secondhandcar.util.UtilMethod;
import com.pgytesting.secondhandcar.vo.Parameters;
import com.pgytesting.secondhandcar.vo.ResponseMessage;

/**
 * 还款账单表(RepaymentBill)表服务实现类
 *
 * @author makejava
 * @since 2019-08-23 16:44:05
 */
@Service("repaymentBillService")
public class RepaymentBillServiceImpl implements RepaymentBillService {
    @Resource
    private RepaymentBillDao repaymentBillDao;
    @Autowired
    private OrderNameDao orderNameDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public ResponseMessage queryById(String id) {
//        loanRepaymentInformationDao.


        return ResponseMessage.successResponse("");
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<RepaymentBill> queryAllByLimit(int offset, int limit) {
        return this.repaymentBillDao.queryAllByLimit(offset, limit);
    }

    /**
     * 条件查询多条数据
     *
     * @param repaymentBill 实例对象
     * @return 对象列表
     */
    @Override
    public ResponseMessage queryAll(RepaymentBill repaymentBill) {
        RepaymentBill bill = new RepaymentBill();
        bill.setLoanRepaymentInformation(repaymentBill.getLoanRepaymentInformation());
        LinkedList<RepaymentBill> result = this.repaymentBillDao.queryAll(bill);
        for (RepaymentBill repaymentBill1 : result) {
            if (repaymentBill1.getRepaymentTime() < UtilMethod.getTodayZero() && repaymentBill1.getStatus() == 0) {
                Long timeOut = UtilMethod.getTodayZero() - repaymentBill1.getRepaymentTime();
                repaymentBill1.setOverdueFine(UtilMethod.penaltyInterest(repaymentBill1.getRepaymentAmount(), (int) (timeOut / 86400000)));
                repaymentBill1.setStatus(3);
            } else if (repaymentBill1.getRepaymentTime() < UtilMethod.getTodayLastMinute() && repaymentBill1.getStatus() == 0) {
                repaymentBill1.setStatus(2);
            }
        }
        return ResponseMessage.successResponse(result, "查询数据成功");
    }

    /**
     * 查询待还款信息
     *
     * @param repaymentBill 实例对象
     * @return 对象列表
     */
    @Override
    public ResponseMessage queryAll() {
    	//LoanRepaymentInformation
    	OrderName orderName = new OrderName();
    	orderName.setOrderStatus(Parameters.ORDER_27012);
    	orderName.setUserNumber(SessionUtil.getSessionMcpUser().getUserNumber());
    	orderName.setStatus(0);
    	List<LoanRepaymentInformation> list = orderNameDao.queryUserLoanInformation(orderName);
    	if(list == null || list.size() == 0) {
    		return ResponseMessage.successResponse("不存在待还款的数据");
    	}
    	
        RepaymentBill bill = new RepaymentBill();
        List<String> loanRepaymentInformations = new ArrayList<String>();
        for(int i = 0;i<list.size();i++) {
        	loanRepaymentInformations.add(list.get(i).getId());
        }
        bill.setLoanRepaymentInformations(loanRepaymentInformations);
        LinkedList<RepaymentBill> result = this.repaymentBillDao.queryAll(bill);
        for (RepaymentBill repaymentBill1 : result) {
            if (repaymentBill1.getRepaymentTime() < UtilMethod.getTodayZero() && repaymentBill1.getStatus() == 0) {
                Long timeOut = UtilMethod.getTodayZero() - repaymentBill1.getRepaymentTime();
                repaymentBill1.setOverdueFine(UtilMethod.penaltyInterest(repaymentBill1.getRepaymentAmount(), (int) (timeOut / 86400000)));
                repaymentBill1.setStatus(3);
            } else if (repaymentBill1.getRepaymentTime() < UtilMethod.getTodayLastMinute() && repaymentBill1.getStatus() == 0) {
                repaymentBill1.setStatus(2);
            }
        }
        return ResponseMessage.successResponse(result, "查询数据成功");
    }
    
    /**
     * 查询已还款信息
     *
     * @param repaymentBill 实例对象
     * @return 对象列表
     */
    @Override
    public ResponseMessage queryAllRepaid() {
    	//LoanRepaymentInformation
    	OrderName orderName = new OrderName();
    	orderName.setOrderStatus(Parameters.ORDER_27012);
    	orderName.setUserNumber(SessionUtil.getSessionMcpUser().getUserNumber());
    	orderName.setStatus(0);
    	List<LoanRepaymentInformation> list = orderNameDao.queryUserLoanInformation(orderName);
    	if(list == null || list.size() == 0) {
    		return ResponseMessage.successResponse("不存在已还款的数据");
    	}
    	
        RepaymentBill bill = new RepaymentBill();
        List<String> loanRepaymentInformations = new ArrayList<String>();
        for(int i = 0;i<list.size();i++) {
        	loanRepaymentInformations.add(list.get(i).getId());
        }
        bill.setLoanRepaymentInformations(loanRepaymentInformations);
        bill.setStatus(1);
        LinkedList<RepaymentBill> result = this.repaymentBillDao.queryAll(bill);
        
        return ResponseMessage.successResponse(result, "查询数据成功");
    }

    /**
     * 新增数据
     *
     * @param repaymentBill 实例对象
     * @return 实例对象
     */
    @Override
    public RepaymentBill insert(RepaymentBill repaymentBill) {
        this.repaymentBillDao.insert(repaymentBill);
        return repaymentBill;
    }

    /**
     * 修改数据
     *
     * @param repaymentBill 实例对象
     * @return 实例对象
     */
    @Override
    public ResponseMessage update(RepaymentBill repaymentBill) {
        // 获取还款ID，进行数据查询
        RepaymentBill bill = repaymentBillDao.queryById(repaymentBill.getId());
        if (bill.getRepaymentTime() < UtilMethod.getTodayZero() && bill.getStatus() == 0) {
            Long timeOut = UtilMethod.getTodayZero() - bill.getRepaymentTime();
            bill.setOverdueFine(UtilMethod.penaltyInterest(bill.getRepaymentAmount(), (int) (timeOut / 86400000)));
        }
        if (repaymentBill.getOverdueFine().equals(bill.getOverdueFine()) &&
                repaymentBill.getRepaymentAmount().equals(bill.getRepaymentAmount())) {
            repaymentBill.setStatus(1);
            repaymentBill.setUpdatedBy(SessionUtil.getSessionMcpUser().getPhoneNumber());
            this.repaymentBillDao.update(repaymentBill);
        } else {
            ResponseMessage.errorResponse("还款金额不正确，请重新输入");
        }
        return ResponseMessage.successResponse("还款成功");
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String id) {
        return this.repaymentBillDao.deleteById(id) > 0;
    }
}